package com.sunfeng.chap2.p13;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2021/10/9  16:28
 */
public class Test13_5_2 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    RandomAccessFile randomAccessFile = new RandomAccessFile("d:\\nio\\a.txt", "rw");
                    FileChannel fileChannel = randomAccessFile.getChannel();
                    System.out.println("b begin");
                    fileChannel.lock(0, 2, false);
                    System.out.println("B end");
                    fileChannel.close();
                    randomAccessFile.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
        Thread.sleep(2000);
        System.out.println("interrupt 已经执行");
        t.interrupt();

    }
}
